using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.Khachhang;

namespace ERP.View
{
    public partial class viewErp_Ql_Khachhang : DevExpress.XtraEditors.XtraForm
    {
        public viewErp_Ql_Khachhang()
        {
            InitializeComponent();
        }

        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int flag = 0;
        public int count = 0;
        //public cs_erp_khohangInfo kho = new cs_erp_khohangInfo();
        public cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
        public cs_erp_khachhangInfo khachhang = new cs_erp_khachhangInfo();

        private void btnLuu_Click(object sender, EventArgs e)
        {
            
        }

        

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            
            ds = this.khachhang.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
            }
        }

        private void btnMoi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            viewERP_Khachhang kh = new viewERP_Khachhang();
            kh.flag = 1;
            kh.user = this.user;
            kh.ShowDialog();
            this.Load_Data();
        }

        private void btnDieuchinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.khachhang.id_kh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                if (this.khachhang.Get_Info() > 0)
                {
                    viewERP_Khachhang kh = new viewERP_Khachhang();
                    kh.flag = 2;
                    kh.user = this.user;
                    kh.khachhang = this.khachhang;
                    kh.ShowDialog();
                    this.Load_Data();
                }
            }
        }

        

        private void viewErp_Ql_Khachhang_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    
                }
                if (userright.updatedata == 0)
                {
                    this.btnDieuchinh.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.khachhang.id_kh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                if (this.khachhang.Get_Info() > 0)
                {
                    this.flag = 4;
                    this.Close();
                }
            }
        }

        private void btnSodo_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.khachhang.id_kh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                if (this.khachhang.Get_Info() > 0)
                {
                    viewERP_SodoKhachhang skh = new viewERP_SodoKhachhang();
                    skh.user = this.user;
                    skh.kh = this.khachhang;
                    skh.flag = 2;
                    skh.ShowDialog();
                }
            }
            
            
        }

        private void grcDanhsach_Click(object sender, EventArgs e)
        {

        }

        private void grcDanhsach_DoubleClick(object sender, EventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.khachhang.id_kh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                if (this.khachhang.Get_Info() > 0)
                {
                    this.flag = 4;
                    this.Close();
                }
            } 
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            this.Load_Data_by_Condition();
        }

        private void ckDienthoai_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckDienthoai.Checked == true)
            {
                this.txtDienthoai.Focus();
                //this.ckNgay.Checked = false;
                this.ckHoten.Checked = false;
            }
        }

        private void ckHoten_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckHoten.Checked == true)
            {
                this.txtHoten.Focus();
                //this.ckNgay.Checked = false;
                this.ckDienthoai.Checked = false;
            }
        }

        private void txtHoten_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        private void txtDienthoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        private void Load_Data_by_Condition()
        {
            DataSet ds = new DataSet("ds");
            string condition = "";
            if (this.ckHoten.Checked == true)
            {
                condition = " tenkhachhang like N'%" + this.txtHoten.Text.Trim() + "%'";
                ds = this.khachhang.Get_List_by_Condition(condition);
            }
            else
            {
                condition = " dienthoai like N'%" + this.txtDienthoai.Text.Trim() + "%'";
                ds = this.khachhang.Get_List_by_Condition(condition);
            }
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
            }
        }
    }
}